package com.leve.control.mapper;

import com.leve.control.domain.SetPermission;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author whongyu
 * @create by 2019/6/17
 */
@Repository
public interface SetPermissionMapper {

    @Select("<script>" +
            "select `set_p_id`,`user_name`,`user_pass`,`user_permission`,`createtime` from ims_levepower_iot_set_permission" +
            "<where>  " +
            "<if test=\"setPId != null and setPId != ''\"> and set_p_id = #{setPId} </if>" +
            "<if test=\"userName != null and userName != ''\"> and user_name = #{userName} </if>" +
            "<if test=\"userPass != null and userPass != ''\"> and user_pass = #{userPass} </if>" +
            "<if test=\"userPermission != null and userPermission != ''\"> and user_permission = #{userPermission} </if>" +
            "<if test=\"createTime != null and createTime != ''\"> and createtime = #{createTime} </if>" +
            "</where>" +
            "<choose>" +
            "   <when test=\"sort != null and sort.trim() != ''\">" +
            "       order by ${sort} ${order}" +
            "   </when>" +
            "<otherwise>" +
            "        order by set_p_id desc" +
            "</otherwise>" +
            "</choose>" +
            "</script>")
    List<SetPermission> findAll(Map<String, Object> map);

    @Insert("insert into ims_levepower_iot_set_permission(`set_p_id`, `user_name`, `user_pass`, `user_permission`, `createtime`)values(#{setPId}, #{userName}, #{userPass}, #{userPermission}, #{createTime})")
    int add(SetPermission setPermission);

    @Select("select `set_p_id`,`user_name`,`user_pass`,`user_permission`,`createtime` from ims_levepower_iot_set_permission where set_p_id = #{setPId}")
    SetPermission findSetPermById(String setPId);

    @Select("delete from ims_levepower_iot_set_permission where set_p_id = #{setPId}")
    int remove(String setPId);

    @Update("<script>" +
            "update ims_levepower_iot_set_permission " +
            "<set>" +
            "<if test=\"userName != null\">`user_name` = #{userName}, </if>" +
            "<if test=\"userPass != null\">`user_pass` = #{userPass}, </if>" +
            "<if test=\"userPermission != null\">`user_permission` = #{userPermission}, </if>" +
            "</set>" +
            " where set_p_id = #{setPId}" +
            "</script>")
    int update(SetPermission setPermission);
}
